package com.chixing.commons;

import com.chixing.pojo.Myorder;
import com.chixing.pojo.Users;
import com.chixing.service.IUsersService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@Component
public class PayMsgWebSocketConsumer {
    @Autowired
    private WebSocketProcess webSocketProcess;
    @Autowired
    private IUsersService usersService;
    @RabbitHandler
    @RabbitListener(queues = "goPayTipQueue")
    public void sendMsgToClientById(Myorder myorder, Channel channel, Message message){
        System.out.println("消息推送服务拿到队列中的消息");
        if(myorder.getMyorderStatus()==0) {
            Integer userId = myorder.getUserId();
            ServerResponse response = usersService.getById(userId);
            Users user = (Users)response.getData();
            String msg = "客户" + user.getUserNickname() + ",您好！您有订单"+myorder.getMyorderNumber()+"即将过期,请及时去支付";
            System.out.println("消费者接收到消息了，准备消息推送给客户端");
            try {
                webSocketProcess.sendAllMessage(msg);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
